package com.gxkj.projects.myshopx.services.impl;

import com.gxkj.common.exceptions.BusinessException;
import com.gxkj.common.exceptions.ValidateException;
import com.gxkj.common.utils.ListPager;
import com.gxkj.common.utils.ValidatorUtil;
import com.gxkj.projects.myshopx.dao.SuppliersDaoImpl;
import com.gxkj.projects.myshopx.entitys.Suppliers;
import com.gxkj.projects.myshopx.entitys.User;
import com.gxkj.projects.myshopx.enums.ErrorCodeEnum;
import com.gxkj.projects.myshopx.enums.SuppliersStates;
import com.gxkj.projects.myshopx.services.SuppliersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Transactional
@Service
public class SuppliersServiceImpl implements SuppliersService {

    Logger LOG = LoggerFactory.getLogger(SuppliersServiceImpl.class);

    @Autowired
    private ValidatorUtil validatorUtil;

    @Autowired
    private SuppliersDaoImpl suppliersDao;

    public void doAddSuppliers(User user, Suppliers entity) throws ValidateException {
        entity.setCreatedAt(new Date());
        entity.setUpdatedAt(new Date());
        entity.setStates(SuppliersStates.NORMAL);
        validatorUtil.validate(entity,true);

        if(suppliersDao.isExitByName(null,entity.getName())){
            BusinessException businessException = new BusinessException(ErrorCodeEnum.BUSSINESS_ERROR.getCode(),"该名称已经存在");
            throw businessException;
        }

        suppliersDao.insert(entity);

    }


    public void doUpdateSuppliers(User user, Suppliers entity) throws ValidateException {
        Suppliers dbentity = this.getById(entity.getId());
        entity.setStates(dbentity.getStates());
        entity.setCreatedAt(dbentity.getCreatedAt());
        entity.setUpdatedAt(new Date());

        validatorUtil.validate(entity,true);
        if(suppliersDao.isExitByName(entity.getId(),entity.getName() )){
            BusinessException businessException = new BusinessException(ErrorCodeEnum.BUSSINESS_ERROR.getCode(),"该名称已经存在");
            throw businessException;
        }
        suppliersDao.updateByMerge(entity);
    }


    public void doDeleteSuppliers(User user, String id) {
        Suppliers dbentity = this.getById(id);
        dbentity.setStates(SuppliersStates.DEL);
        suppliersDao.updateByMerge(dbentity);
    }


    public ListPager<Suppliers> doPageHQL(int pagenNo, int pageSize, Suppliers entity)   {
        return suppliersDao.doPageHQL(pagenNo,pageSize,entity);
    }


    public Suppliers getById(String id) {
        Suppliers dbentity = suppliersDao.selectById(id,Suppliers.class);
        return dbentity;
    }
    @Transactional(propagation= Propagation.REQUIRED,rollbackForClassName="Exception")
    public void doAddTrans(){
        Suppliers entity =  new Suppliers();
        entity.setName("test");
        entity.setCreatedAt(new Date());
        entity.setUpdatedAt(new Date());
        entity.setStates(SuppliersStates.NORMAL);

        suppliersDao.insert(entity);

        Suppliers entity2 =  new Suppliers();
       entity2.setName("testtesttesttesttesttesttesttesttesttesttesttesttesttest");
        entity2.setCreatedAt(new Date());
        entity2.setUpdatedAt(new Date());
        entity2.setStates(SuppliersStates.NORMAL);
        suppliersDao.insert(entity2);
    }
}
